package com.yy.wga.goods.service.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.common.util.common.JsonUtils;
import com.yy.wga.goods.service.api.HeadSupplierProductApi;
import com.yy.wga.goods.service.api.PromoterProductApi;
import com.yy.wga.goods.service.api.request.product.GetHeadSupplierProductDetailReq;
import com.yy.wga.goods.service.api.request.product.GetHeadSupplierProductListReq;
import com.yy.wga.goods.service.api.request.product.GetProductPromotionInfoReq;
import com.yy.wga.goods.service.api.request.product.GetPromoteProductDetailReq;
import com.yy.wga.goods.service.api.request.product.GetPromoteProductListReq;
import com.yy.wga.goods.service.api.response.ApiResult;
import com.yy.wga.goods.service.api.response.product.GetHeadSupplierProductDetailDTO;
import com.yy.wga.goods.service.api.response.product.GetHeadSupplierProductListDTO;
import com.yy.wga.goods.service.api.response.product.ProductPromotionInfoLinkDTO;
import com.yy.wga.goods.service.api.response.product.ProductPromotionQrcodeInfoDTO;
import com.yy.wga.goods.service.api.response.product.PromoteProductDetailDTO;
import com.yy.wga.goods.service.api.response.product.PromoteProductListDTO;
import com.yy.wga.goods.service.api.response.product.PromoterSingleProductPromotionInfoDTO;
import com.yy.wga.goods.service.service.ProductService;
import com.yy.wga.wechat.feign.api.WechatMpFeign;
import com.yy.wga.wechat.feign.response.WechatGetAccessTokenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private PromoterProductApi promoterProductApi;
    @Resource
    private HeadSupplierProductApi headSupplierProductApi;
    @Resource
    private WechatMpFeign wechatMpFeign;


    @Override
    @Retryable(value = {Exception.class}, maxAttempts = 3)
    public PromoteProductListDTO getPromoteProductList(GetPromoteProductListReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);
        log.info("invoke getPromoteProductList req:{}", dataMap);
        JSONObject jsonObject = promoterProductApi.getPromoteProductList(accessToken, dataMap);
        log.info("invoke getPromoteProductList resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, PromoteProductListDTO.class);
    }

    @Override
    @Retryable(value = {Exception.class}, maxAttempts = 3)
    public PromoteProductDetailDTO getPromoteProductDetail(GetPromoteProductDetailReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);

        log.info("invoke getPromoteProductDetail req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = promoterProductApi.getPromoteProductDetail(accessToken, dataMap);
        log.info("invoke getPromoteProductDetail resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, PromoteProductDetailDTO.class);
    }

    @Retryable(value = {Exception.class}, maxAttempts = 3)
    public ProductPromotionInfoLinkDTO getProductPromotionLinkInfo(GetProductPromotionInfoReq getProductPromotionInfoReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getProductPromotionInfoReq), Map.class);

        log.info("invoke getProductPromotionInfo req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = promoterProductApi.getProductPromotionLinkInfo(accessToken, dataMap);
        log.info("invoke getProductPromotionInfo resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, ProductPromotionInfoLinkDTO.class);
    }

    @Override
    public ProductPromotionQrcodeInfoDTO getProductPromotionQrcodeInfo(GetProductPromotionInfoReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);

        log.info("invoke getProductPromotionQrcodeInfo req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = promoterProductApi.getProductPromotionQrcodeInfo(accessToken, dataMap);
        log.info("invoke getProductPromotionQrcodeInfo resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, ProductPromotionQrcodeInfoDTO.class);
    }

    @Override
    public PromoterSingleProductPromotionInfoDTO getPromoterSingleProductPromotionInfo(GetProductPromotionInfoReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);

        log.info("invoke getPromoterSingleProductPromotionInfo req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = promoterProductApi.getPromoterSingleProductPromotionInfo(accessToken, dataMap);
        log.info("invoke getPromoterSingleProductPromotionInfo resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, PromoterSingleProductPromotionInfoDTO.class);
    }

    @Override
    public GetHeadSupplierProductDetailDTO getHeadSupplierProductDetail(GetHeadSupplierProductDetailReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);

        log.info("invoke getHeadSupplierProductDetail req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = headSupplierProductApi.getHeadSupplierProductDetail(accessToken, dataMap);
        log.info("invoke getHeadSupplierProductDetail resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, GetHeadSupplierProductDetailDTO.class);
    }

    @Override
    public GetHeadSupplierProductListDTO getHeadSupplierProductList(GetHeadSupplierProductListReq getPromoteProductListReq) {
        Result<WechatGetAccessTokenResponse> accessTokenResult = wechatMpFeign.getChannelAccessToken();
        if (!accessTokenResult.isSuccess()) {
            throw new BusinessException(accessTokenResult.getMessage());
        }
        WechatGetAccessTokenResponse getAccessTokenResponse = accessTokenResult.getData();
        String accessToken = getAccessTokenResponse.getAccessToken();

        Map<String, Object> dataMap = JSONObject.parseObject(JsonUtils.toJSONString(getPromoteProductListReq), Map.class);

        log.info("invoke getHeadSupplierProductList req:{}", JSONObject.toJSONString(dataMap));
        JSONObject jsonObject = headSupplierProductApi.getHeadSupplierProductList(accessToken, dataMap);
        log.info("invoke getHeadSupplierProductList resp:{}", JSONObject.toJSONString(jsonObject));

        return ApiResult.handle(jsonObject, GetHeadSupplierProductListDTO.class);
    }
}
